package com.yiwei.hdb.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;


import com.yiwei.hdb.R;
import com.yiwei.hdb.util.DateTime;

import java.util.Calendar;
import java.util.Date;


/**
 * Created by Administrator on 2016/4/1 0001.
 */
public class TimeView extends View {
    int windowWidth;
    int windowHeight;
    private Calendar cal;

    private Calendar autoCal;
    private Paint titlePaint;
    private Paint titleBackgroundPaint;
    private Paint mainPaint;
    private Paint OneToSevenPaint;
    private Bitmap GiftBitmap;
    private int resHight;
    private int titleHeight;
    private int isSunnday = 0;
    private RectF titleRect;
    private Date nowDay;
    private Paint titleWhitePaint;
    private int y30;
    private int y76;
    private Bitmap SignBitmap;
    private int x50;
    private BitmapFactory.Options options;
    private int y2;

    public TimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TimeView);
        try {
            titleSize = attributes.getDimensionPixelSize(R.styleable.TimeView_mainTitleTextSize, 18);
            contentTextSize = attributes.getDimensionPixelSize(R.styleable.TimeView_titleTextSize, 18);

        } catch (Exception e) {

        } finally {
            attributes.recycle();
        }
        y2 = 2;
        y30 = getResources().getDimensionPixelSize(R.dimen.y30);
        x50 = getResources().getDimensionPixelSize(R.dimen.x50);
        y76 = getResources().getDimensionPixelSize(R.dimen.y76);

        titlePaint = new Paint();
        titlePaint.setTextSize(contentTextSize);
        titlePaint.setColor(titleTextColor);
        titlePaint.setAntiAlias(true);

        titleWhitePaint = new Paint();
        titleWhitePaint.setTextSize(contentTextSize);
        titleWhitePaint.setColor(titleTextWhiteColor);
        titleWhitePaint.setAntiAlias(true);
        titleBackgroundPaint = new Paint();
        titleBackgroundPaint.setColor(titleBackColor);
        mainPaint = new Paint();
        mainPaint.setColor(titleTextColor);
        mainPaint.setTextSize(titleSize);
        OneToSevenPaint = new Paint();
        OneToSevenPaint.setColor(0xff888888);
        OneToSevenPaint.setTextSize(titleSize);
        OneToSevenPaint.setAntiAlias(true);
//        pinkPaint = new Paint();
//        pinkPaint.setColor(Color.parseColor("#ffd1d1"));
//        pinkPaint.setAntiAlias(true);
        options = new BitmapFactory.Options();
        options.outWidth = x50;
        options.outHeight = x50;


        SignBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_sign_finish, options);
//        SignBitmap.setWidth();
        titleRect = new RectF();
        nowDay = new Date();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        windowWidth = w;
        windowHeight = h;
    }

    public int getWindowWidth(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    public int getWindowHeight(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        isSunnday = 0;
        cal = Calendar.getInstance();
        nowDay.setTime(time);
        cal.setTime(nowDay);

        autoCal = Calendar.getInstance();
        autoCal.setTime(nowDay);
        canvas.drawText(cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月", windowWidth / 2 - mainPaint.measureText(DateTime.toMonthTime(time)) / 2, mainPaint.measureText("五") + y30, mainPaint);
        for (int i = 0; i < 7; i++) {
            canvas.drawText(weekString[i], (i + 1) * windowWidth / 8 - mainPaint.measureText("五") / 2, mainPaint.measureText("五") + titlePaint.measureText("五") + y76, OneToSevenPaint);
        }
//        剩下的高度
        resHight = (int) (windowHeight - (mainPaint.measureText("五") + titlePaint.measureText("五") + y76 + titlePaint.measureText("五") / 2));
        titleHeight = (int) (mainPaint.measureText("五") + titlePaint.measureText("五") + y76 + titlePaint.measureText("五") / 2);
//       获取有多少天
        int DaysofMonth = cal.getActualMaximum(Calendar.DATE);
//***************************

        for (int i = 1; i <= DaysofMonth; i++) {
            autoCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), i);
            int num = autoCal.get(Calendar.DAY_OF_WEEK);
            if (num == Calendar.SUNDAY && i != 1) {
                isSunnday++;
            }

            switch (num) {
                case Calendar.SUNDAY:
//                    drawOval(canvas, 1, i);
                    drawBitmap1(canvas, GiftBitmap, 1, i);
                    drawBitmap(canvas, 1, i);
                    drawText(canvas, 1, i);


                    break;
                case Calendar.MONDAY:
//                    drawOval(canvas, 2, i);

                    drawBitmap1(canvas, GiftBitmap, 2, i);
                    drawBitmap(canvas, 2, i);
                    drawText(canvas, 2, i);


                    break;
                case Calendar.TUESDAY:
//                    drawOval(canvas, 3, i);
                    drawBitmap1(canvas, GiftBitmap, 2, i);
                    drawBitmap(canvas, 3, i);
                    drawText(canvas, 3, i);


                    break;
                case Calendar.WEDNESDAY:
//                    drawOval(canvas, 4, i);
                    drawBitmap1(canvas, GiftBitmap, 4, i);
                    drawBitmap(canvas, 4, i);
                    drawText(canvas, 4, i);


                    break;
                case Calendar.THURSDAY:
//                    drawOval(canvas, 5, i);
                    drawBitmap1(canvas, GiftBitmap, 5, i);
                    drawBitmap(canvas, 5, i);
                    drawText(canvas, 5, i);


                    break;
                case Calendar.FRIDAY:
//                    drawOval(canvas, 6, i);
                    drawBitmap1(canvas, GiftBitmap, 6, i);
                    drawBitmap(canvas, 6, i);
                    drawText(canvas, 6, i);


                    break;
                case Calendar.SATURDAY:
//                    drawOval(canvas, 7, i);
                    drawBitmap1(canvas, GiftBitmap, 7, i);
                    drawBitmap(canvas, 7, i);
                    drawText(canvas, 7, i);
                    break;
            }
        }

    }


    //    long time = new Date().getTime();
    long time = (long) 1459821033 * (long) 1000;
    int titleBackColor = Color.parseColor("#ededed");
    float titleSize = 18;
    int titleTextColor = 0xff111111;
    int titleTextWhiteColor = Color.WHITE;
    float contentTextSize = 18;
    int signday = -1;
    int signToday = -1;
    String[] weekString = new String[]{"日", "一", "二", "三", "四", "五", "六"};
    Integer[] signnum;


    public void setTime(long time, int signday, Integer[] signnum, int signToday) {
        this.time = time;
        this.signday = signday;
        this.signnum = signnum;
        this.signToday = signToday;
        GiftBitmap = drawTextToBitmap(getContext(), R.mipmap.img_sign_today, signday + "");
        postInvalidate();
    }


    public void signGift(int signday) {
        this.signday = signday;
    }

    private void drawText(Canvas canvas, int one_to_seven, int i) {
        if (i < 10) {
            if (i == signToday) {
//                canvas.drawText(i + ""
//                        , one_to_seven * windowWidth / 8 - titlePaint.measureText("五") / 2
//                        , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("五") + resHight / 6 / 2
//                        , titlePaint);
            } else {
                canvas.drawText(" " + i
                        , one_to_seven * windowWidth / 8 - titlePaint.measureText("五") / 2
                        , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("五") + resHight / 6 / 2-y2
                        , titlePaint);
            }
        } else {
            if (i == signToday) {
//                canvas.drawText(i + ""
//                        , one_to_seven * windowWidth / 8 - titlePaint.measureText("五") / 2
//                        , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("五") + resHight / 6 / 2
//                        , titlePaint);
            } else {
                canvas.drawText(i + ""
                        , one_to_seven * windowWidth / 8 - titlePaint.measureText("五") / 2
                        , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("五") + resHight / 6 / 2-y2
                        , titlePaint);
            }

        }

    }


    private void drawBitmap(Canvas canvas, int one_to_seven, int i) {
        if (signnum != null) {
            for (int j = 0, size = signnum.length; j < size; j++) {
                if (i == signnum[j]) {
                    if (i == signToday) {
                        if (GiftBitmap != null)
                            canvas.drawBitmap(GiftBitmap
                                    , one_to_seven * windowWidth / 8 - GiftBitmap.getWidth() / 2
                                    , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("20") / 2 + resHight / 6 / 2 - GiftBitmap.getHeight() / 2
                                    , titlePaint);
                        break;
                    }

                    canvas.drawBitmap(SignBitmap
                            , one_to_seven * windowWidth / 8 - SignBitmap.getWidth() / 2
                            , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("20") / 2 + resHight / 6 / 2 - SignBitmap.getHeight() / 2
                            , titlePaint);


                    break;
                }
            }
        }
    }

    private void drawBitmap1(Canvas canvas, Bitmap bitmap, int one_to_seven, int i) {
//        if (i == signday) {
//            canvas.drawBitmap(bitmap
//                    , one_to_seven * windowWidth / 8 - bitmap.getWidth() / 2
//                    , titleHeight + resHight / 6 * isSunnday + titlePaint.measureText("20") / 2 + resHight / 6 / 2 - bitmap.getHeight() / 2
//                    , titlePaint);
//        }
    }

    public Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
        try {
            Resources resources = mContext.getResources();
            float scale = resources.getDisplayMetrics().density;
            Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId, options);

            android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
            // set default bitmap config if none
            if (bitmapConfig == null) {
                bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
            }
            // resource bitmaps are imutable,
            // so we need to convert it to mutable one
            bitmap = bitmap.copy(bitmapConfig, true);

            Canvas canvas = new Canvas(bitmap);
            // new antialised Paint
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            // text color - #3D3D3D
            paint.setColor(0xffffffff);
            // text size in pixels
            paint.setTextSize(titleSize);
            // text shadow
//            paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

            // draw text to the Canvas center
            Rect bounds = new Rect();
            paint.getTextBounds(mText, 0, mText.length(), bounds);
            int x = (bitmap.getWidth() - bounds.width()) / 6;
            int y = (bitmap.getHeight() + bounds.height()) / 5;
//            int y = bitmap.getHeight() / 2 - bounds.height()/2;
            canvas.drawText(mText, x * scale, y * scale, paint);

            return bitmap;
        } catch (Exception e) {
            // TODO: handle exception


            return null;
        }

    }

}
